package com.swufe.week6;

import androidx.annotation.NonNull;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.SimpleAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class listActivity4 extends ListActivity implements Runnable {
    private ArrayList<HashMap<String,String>>listItems;
    private  static  final  String TAG="listActivity4";
    protected SimpleAdapter listItemAdapter;
    Handler handler;
    private Object MyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initListView();
        //RateAdapter myAdapter = new RateAdapter(this, R.layout.my_list, listItems);
        this.setlistItemAdapter(MyAdapter);
        Thread thread = new Thread(this);
        thread.start();


        // setContentView(R.layout.activity_list4);

        // ListView list3 = findViewById(R.id.mylist1);
        // listItems = new ArrayList<HashMap<String, String>>();

        handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 9) {
                    ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    SimpleAdapter ListItemAdapter = new SimpleAdapter(listActivity4.this, list,
                            R.layout.my_list,
                            new String[]{"itemTitle", "itemDetail"},
                            new int[]{R.id.itemTitle, R.id.itemDetail}
                    );
                    setListAdapter(listItemAdapter);
                    //Toast.makeText(listActivity4.this,"ret size="+list.size(),Toast.LENGTH_SHORT).show();
                }
                super.handleMessage(msg);
            }
        };
    }

    private void setlistItemAdapter(Object myAdapter) {
        //listItemAdapter= (SimpleAdapter) MyAdapter;
    }


    // private void setlistItemAdapter(MyAdapter myAdapter) {
   // }

    protected void initListView(){
        //ListView list3 = findViewById(R.id.mylist1);
        listItems = new ArrayList<HashMap<String, String>>();
        for(int i=0;i<10;i++){
            HashMap<String,String>map=new HashMap<String,String>();
            map.put("itemTitle","rate:"+i);
            map.put("itemDetail","detail:"+i);
            listItems.add(map);
        }
        listItemAdapter = new SimpleAdapter(this, listItems,
                R.layout.my_list,
                new String[]{"itemTitle", "itemDetail"},
                new int[]{R.id.itemTitle,R.id.itemDetail});
        //list3.setAdapter(listItemAdapter);

    }
    @Override
    public void run() {
        Log.i(TAG, "run: .........");
        //URL url=null;
        boolean marker=false;
        List<HashMap<String,String>> retlist =new ArrayList<HashMap<String,String>>();
        try {
            String url = "http://www.usd-cny.com/bankofchina.htm";
            Document doc = Jsoup.connect(url).get();
            Log.i(TAG, "run: " + doc.title());
            Element tables = doc.getElementsByTag("table").first();
            Elements trs=tables.getElementsByTag("tr");
            for(Element tr:trs){
                Elements tds=tr.getElementsByTag("td");
                if(tds.size()>0){
                    String str =tds.first().text();
                    String val=tds.get(5).text();
                    HashMap<String,String>map=new HashMap<String,String>();
                    map.put("itemTitle",str);
                    map.put("itemDetail",val);
                    retlist.add(map);
                    Log.i(TAG, "run: td="+str);
                    Log.i(TAG, "run: rate="+val);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回给主线程
        Message msg = handler.obtainMessage(9,retlist);
        msg.obj=retlist;
        handler.sendMessage(msg);
    }


}